package mapper

import (
	"context"
	"gorm.io/gorm"
	"mall/services/product/rpc/internal/domain/bo"
	"mall/services/product/rpc/internal/domain/do"
	"mall/services/product/rpc/internal/initialize/mysql"
	"mall/services/product/rpc/internal/mapper/entity"
)

var categoryBrandAllColumn = "id, create_time, update_time, brand_id, category_id"

type CategoryBrandMapper struct {
	ctx context.Context
}

func NewCategoryBrandMapper(ctx context.Context) *CategoryBrandMapper {
	return &CategoryBrandMapper{ctx: ctx}
}

func (m *CategoryBrandMapper) SelectCountCtx(db *gorm.DB, categoryBO bo.CategoryBrandBO) (totalCount int64, err error) {
	// 监控
	_, span := mysql.StartSpan(m.ctx, "CategoryBrandMapper#SelectCountCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := db.Session(&gorm.Session{SkipDefaultTransaction: true})

	var total int64
	brandEntity := entity.CategoryBrandEntity{}
	tx = tx.Select(categoryBrandAllColumn).Table(brandEntity.TableName())
	m.getAllWhereSql(tx, categoryBO)
	tx.Count(&total)
	return total, tx.Error
}
func (m *CategoryBrandMapper) SelectOneCtx(db *gorm.DB, categoryBO bo.CategoryBrandBO) (categoryEntity entity.CategoryBrandEntity, err error) {
	// 监控
	_, span := mysql.StartSpan(m.ctx, "CategoryBrandMapper#SelectOneCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := db.Session(&gorm.Session{SkipDefaultTransaction: true})

	brandEntity := entity.CategoryBrandEntity{}
	tx = tx.Select(categoryBrandAllColumn).Table(brandEntity.TableName())
	m.getAllWhereSql(tx, categoryBO)
	tx.First(&brandEntity)
	return brandEntity, tx.Error
}
func (m *CategoryBrandMapper) SelectListCtx(db *gorm.DB, categoryBO bo.CategoryBrandBO) (categoryEntity []entity.CategoryBrandEntity, err error) {
	// 监控
	_, span := mysql.StartSpan(m.ctx, "CategoryBrandMapper#SelectListCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := db.Session(&gorm.Session{SkipDefaultTransaction: true})

	var brandEntityArr []entity.CategoryBrandEntity
	tx = tx.Select(categoryBrandAllColumn).Table("category_brand")
	m.getAllWhereSql(tx, categoryBO)
	tx = tx.Find(&brandEntityArr)
	return brandEntityArr, tx.Error
}

func (m *CategoryBrandMapper) getAllWhereSql(db *gorm.DB, categoryBO bo.CategoryBrandBO) {
	if categoryBO.Id > 0 {
		db.Where("id = ?", categoryBO.BrandId)
	}
	if categoryBO.BrandId > 0 {
		db.Where("brand_id = ?", categoryBO.BrandId)
	}
	if len(categoryBO.BrandIdArr) > 0 {
		db.Where("brand_id in ?", categoryBO.BrandIdArr)
	}
	if categoryBO.CategoryId > 0 {
		db.Where("category_id = ?", categoryBO.CategoryId)
	}
	if categoryBO.StartTime != nil {
		db.Where("create_time = ?", categoryBO.StartTime)
	}
	if categoryBO.EndTime != nil {
		db.Where("create_time = ?", categoryBO.EndTime)
	}
}

func (m *CategoryBrandMapper) InsertCtx(engin *gorm.DB, brandEntity entity.CategoryBrandEntity) (err error) {
	// 监控
	_, span := mysql.StartSpan(m.ctx, "CategoryBrandMapper#InsertCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := engin.Create(&brandEntity)
	return tx.Error
}

func (m *CategoryBrandMapper) DeleteCtx(engin *gorm.DB, brandId int64) (err error) {
	// 监控
	_, span := mysql.StartSpan(m.ctx, "CategoryBrandMapper#DeleteCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	categoryBrandEntity := entity.CategoryBrandEntity{}
	// db.Where("id in ?", []int{18, 19, 20}).Delete(&TestUser{})
	tx := engin.Where("brand_id = ?", brandId).Delete(&categoryBrandEntity)
	return tx.Error
}

func (m *CategoryBrandMapper) SelectCategoryCtx(engin *gorm.DB, brandIdArr []int64) (doArr []do.CategoryDO, err error) {
	// 监控
	_, span := mysql.StartSpan(m.ctx, "CategoryBrandMapper#SelectCategoryCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := engin.Session(&gorm.Session{SkipDefaultTransaction: true})
	var returnArr []do.CategoryDO
	AllColumnJoin := "t2.category_id as category_id,category_brand.brand_id,t2.shop_id as shop_id," +
		"t2.parent_id as parent_id,t2.name as name,t2.desc as `desc`,t2.path as path," +
		"t2.status as status,t2.icon as icon,t2.img_url as img_url,t2.level as level,t2.seq as seq"
	tx = tx.Select(AllColumnJoin).
		Table("category_brand").
		Joins("left join category t2 on category_brand.category_id = t2.category_id").
		Where("category_brand.brand_id in ?", brandIdArr).Find(&returnArr)
	return returnArr, tx.Error
}
